package com.project.class4_6;

public class DieTest {

    public static void main(String[] args) {
        MyObj m1 = new MyObj("郭靖");
        MyObj m2 = new MyObj("黄蓉");

        new Thread(){
            public void run(){
                synchronized (m1){
                    try {
                        Thread.sleep(500);
                        m2.speak();
                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                }
            }
        }.start();

        new Thread(){
            public void run(){
                synchronized (m2){
                    try {
                        Thread.sleep(500);
                        m1.speak();
                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                }
            }
        }.start();
    }
}

class  MyObj{
    private String name;
    public MyObj(String name){
        this.name = name;
        System.out.println("创建"+name+"对象");
    }
    public synchronized void speak(){
        System.out.println("执行"+name+"speak方法");
    }
}